package org.crazyit.cloud;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author JLL
 * @Date 2018/8/11/0011 下午 2:38
 *
 * 提供服务：接到电话派出一个警察
 */
@RestController
public class PoliceController {
    @Autowired
    private DiscoveryClient discoveryClient;


    @RequestMapping(value = "/call/{id}",method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE)
    public Police call(@PathVariable("id") Integer policeId, HttpServletRequest request){
        Police police = new Police();
        police.setId(policeId);
        police.setName("angus");
        police.setMessage(request.getRequestURL().toString());
        return police;
    }

    @GetMapping("/meta")
    @ResponseBody
    public String getMetadata(){
        List<ServiceInstance> instances = discoveryClient.getInstances("ek-provider");
        for(ServiceInstance ins : instances) {
            String name = ins.getMetadata().get("company-name");
            System.out.println(ins.getPort() + "---" + name);
        }
        return "";
    }



}
